Release 10.1A: OpenEdge Development:
Messaging and ESB


Publishing and receiving a group of messages in a transaction

The procedures example22.p and example23.p (1 of 2) publish and receive a group of messages in a single transaction. The procedure example22.p creates a session that is transacted for sending, and The procedure example23.p (1 of 2) creates a session that is transacted for receiving.

To publish and receive a group of messages in a transaction:

  1. Run example23.p (1 of 2) so the subscriber is running before you publish, as shown:
  2. example23.p
    /* Subscribes and receives three messages in a single transaction. */ 
    DEFINE VARIABLE pubsubsession AS HANDLE. 
    DEFINE VARIABLE consumerH AS HANDLE. 
    DEFINE VARIABLE msgNum AS INT INIT 0. 
    /* Creates a transaction for receiving session. */ 
    RUN jms/pubsubsession.p PERSISTENT SET pubsubsession  
                                      ("-H localhost -S 5162 "). 
    RUN setBrokerURL IN pubsubsession ("localhost:2506"). 
    RUN setTransactedReceive IN pubsubsession. 
    RUN beginSession IN pubsubsession. 
    /* Subscribe to the TestTopic topic. Messages are handled by the 
       "msgHandler" internal procedure. 
    */ 
    RUN createMessageConsumer IN pubsubsession ( 
                   THIS-PROCEDURE,   /* This proc will handle it */ 
                   "msgHandler",     /* name of internal procedure */ 
                   OUTPUT consumerH). 
    RUN subscribe IN pubsubsession (
                        "TestTopic",  /* name of topic */ 
                        ?,            /* Subscription is not durable */ 
                        ?,            /* No message selector */ 
                        no,           /* Want my own messages too */ 
                        consumerH).   /* Handles the incoming messages*/ 
    /* Start receiving messages */ 
    RUN startReceiveMessages IN pubsubsession. 
    /* Wait to receive the three messages. */ 
    WAIT-FOR u1 OF THIS-PROCEDURE. 
    
    PROCEDURE msgHandler: 
    DEFINE INPUT PARAMETER messageH AS HANDLE.  
    DEFINE INPUT PARAMETER msgConsumerH AS HANDLE. 
    DEFINE OUTPUT PARAMETER replyH AS HANDLE. 
        /* Display the message - we assume that reply is not required. */ 
        DISPLAY "Message text: " DYNAMIC-FUNCTION('getText':U IN messageH) 
          FORMAT "x(70)". 
        RUN deleteMessage IN messageH. 
        msgNum = msgNum + 1. 
        /* Commit the reception of the three messages. */ 
        IF msgNum = 3 THEN 
        DO: 
          RUN commitReceive IN pubsubsession. 
          message "committed!". 
          APPLY "U1" TO THIS-PROCEDURE.  
        END. 
    END. 
    

  3. Run example22.p to subscribe and receive messages from example22.p in a single transaction, as shown:
  4. example22.p 
    /* Publishes A group of Text messages in a single transaction. */ 
    DEFINE VARIABLE pubsubsession AS HANDLE. 
    DEFINE VARIABLE messageH AS HANDLE. 
    /* Creates a transcated for sending session. */ 
    RUN jms/pubsubsession.p PERSISTENT SET pubsubsession  
                                      ("-H localhost -S 5162 "). 
    RUN setBrokerURL IN pubsubsession ("localhost:2506"). 
    RUN setTransactedSend IN pubsubsession. 
    RUN beginSession IN pubsubsession. 
    /* Create a text message */ 
    RUN createTextMessage IN pubsubsession (OUTPUT messageH).  
    /* Publish three messages */ 
    RUN setText IN messageH ("message1"). 
    RUN publish IN pubsubsession ("TestTopic", messageH, ?, ?, ?). 
    RUN setText IN messageH ("message2"). 
    RUN publish IN pubsubsession ("TestTopic", messageH, ?, ?, ?). 
    RUN setText IN messageH ("message3"). 
    RUN publish IN pubsubsession ("TestTopic", messageH, ?, ?, ?). 
    /* Commit the publication of the messages. */ 
    RUN commitSend IN pubsubsession. 
    RUN deleteMessage IN messageH. 
    RUN deleteSession IN pubsubsession. 
    


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095